package com.shinewind.payment.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shinewind.payment.mapper.IUserAddressMapper;
import com.shinewind.payment.service.IUserAddressService;
import com.shinewind.pojo.dto.UserAddressDto;
import com.shinewind.pojo.entity.UserAddress;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserAddressServiceImpl extends ServiceImpl<IUserAddressMapper, UserAddress> implements IUserAddressService {
    @Override
    public List<UserAddress> getUserAddressByUid(String uId) {
        List<UserAddress> userAddresses = this.lambdaQuery().eq(UserAddress::getUId,uId).list();
        return userAddresses;
    }

    @Override
    public Boolean saveUserAddressByuId(String uId, UserAddressDto userAddressDto) {
        //判断该用户是否有默认地址
        Long hasDefault = this.lambdaQuery().eq(UserAddress::getUId,uId).eq(UserAddress::getIsDefault,1).count();
        UserAddress userAddress = UserAddress.builder().uId(uId).build();
        BeanUtils.copyProperties(userAddressDto,userAddress);
        //有默认地址,则新增地址为普通地址
        if(hasDefault > 0){
            userAddress.setIsDefault(0L);
        }
        //没有默认地址,此次新增地址为默认地址
        else if(hasDefault == 0){
            userAddress.setIsDefault(1L);
        }
        Boolean flag = this.save(userAddress);
        return flag;
    }

    @Override
    public Boolean updateUserAddressByaddressId(String addressId, UserAddressDto userAddressDto) {
        UserAddress userAddress = UserAddress.builder().build();
        BeanUtils.copyProperties(userAddressDto,userAddress);
        return this.lambdaUpdate().eq(UserAddress::getAddressId,addressId).update(userAddress);
    }

    @Override
    public Boolean updateDefaultAddressByaddressId(String addressId) {
        //根据地址id查询用户id
        UserAddress userAddress = this.lambdaQuery().eq(UserAddress::getAddressId,addressId).one();
        String uId = userAddress.getUId();
        String oldAddressId = this.lambdaQuery().eq(UserAddress::getUId,uId).eq(UserAddress::getIsDefault,1).one().getAddressId();
        //查询默认地址总数,为1才修改
        Long total = this.lambdaQuery().eq(UserAddress::getUId,uId).eq(UserAddress::getIsDefault,1).count();
        if(total == 1){
            System.out.println("addressId:"+addressId);
            System.out.println("uId:"+uId);
            System.out.println("oldAddressId:"+oldAddressId);
            //修改以前的地址为普通地址
            this.lambdaUpdate().eq(UserAddress::getAddressId,oldAddressId).eq(UserAddress::getUId,uId)
                    .set(UserAddress::getIsDefault,0).update();
            //修改现在的为默认地址
            this.lambdaUpdate().eq(UserAddress::getAddressId,addressId).set(UserAddress::getIsDefault,1).update();
            return true;
        }
        return false;
    }

    @Override
    public Boolean deleteAddressByaddressId(String addressId) {
        Boolean flag = this.lambdaUpdate().eq(UserAddress::getAddressId,addressId).remove();
        return flag;
    }

    @Override
    public String getUidByAddressId(String addressId) {
        return this.lambdaQuery().eq(UserAddress::getAddressId,addressId).one().getUId();
    }
}
